using System;
using System.Collections.Generic;
using System.Text;

namespace ClientTools.Client.Library.Provider
{
    public class DatabaseProvider: Interfaces.IProvider, Interfaces.ILogging, Interfaces.IStats, Interfaces.IVersions 
    {

        private string _ConnectionString;
        private ClientTools.Library.Logging.LogManager _LogManager;
        private ClientTools.Library.Stats.StatsManager _StatsManager;
        private ClientTools.Library.Versions.VersionManager _VersionManager;

        public string ConnectionString
        {
            get { return _ConnectionString; }
            set { _ConnectionString = value; this.InitObjects(); }
        }

        public DatabaseProvider()
        {
        }

        private void InitObjects() 
        {
            _LogManager = new ClientTools.Library.Logging.LogManager(_ConnectionString);
            _StatsManager = new ClientTools.Library.Stats.StatsManager(_ConnectionString);
            _VersionManager = new ClientTools.Library.Versions.VersionManager(_ConnectionString);
        }

        public DatabaseProvider(string connectionString) 
        {
            _ConnectionString = connectionString;
            this.InitObjects();
        }

        #region I_Logging Member

        public ClientTools.Library.Logging.LogInfoCollection GetLogInfos()
        {
            ClientTools.Library.Logging.LogInfoCollection returnValue = new ClientTools.Library.Logging.LogInfoCollection();
            foreach (ClientTools.Library.Logging.LogInfo li in _LogManager.GetLogInfos())
            {
                returnValue.Add(li);
            }
            return returnValue;
        }

        public ClientTools.Library.Logging.LogCollection GetLogs(DateTime? startDate, DateTime? endDate, string logHeader, int? userId, int? nodeId, int? maxRows, string comment)
        {
            return _LogManager.GetLogs(startDate, endDate, logHeader, userId, nodeId, maxRows,comment);
        }

        public int DeleteLogsByIDs(int[] logIds)
        {
            return _LogManager.DeleteLogsByIDs(logIds);
        }

        public int DeleteLogsByHeader(string logHeader)
        {
            return _LogManager.DeleteLogsByHeader(logHeader);
        }

        public int DeleteLogsByDate(DateTime date)
        {
            return _LogManager.DeleteLogsByDate(date);
        }

        #endregion

        #region I_Stats Member

        public ClientTools.Library.Stats.StatsInfo DeleteStatsByDate(DateTime date)
        {
            return _StatsManager.DeleteStatsByDate(date);
        }

        public ClientTools.Library.Stats.StatsInfo GetStatsInfo()
        {
            return _StatsManager.GetStatsInfo();
        }

        #endregion

        #region I_Versions Member

        public ClientTools.Library.Versions.VersionInfo GetVersionInfos()
        {
            return _VersionManager.GetVersionInfos();
        }

        public ClientTools.Library.Versions.VersionInfo DeleteVersionsByDate(DateTime date)
        {
            return _VersionManager.DeleteVersionsByDate(date);
        }

        public ClientTools.Library.Versions.VersionInfo DeleteVersionsByNumberOfItems(int numberOfItems)
        {
            return _VersionManager.DeleteVersionsByNumberOfItems(numberOfItems);
        }

        public ClientTools.Library.Versions.ContentNodeCollection GetNodes(int parentId, bool recursive)
        {
            return _VersionManager.GetNodes(parentId, recursive); 
        }

        #endregion

        #region I_Provider Member

        public string Providername
        {
            get { return "Database Provider"; }
        }

        #endregion

    }
}
